package com.yzh.pinboard.BoardView;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by OCQIT500 on 2017/3/1.
 */

public class CommonMethod {


    public static Object cloneObject(Object Obj) {
        return derializate(serializate(Obj));
    }


    private static byte[] serializate(Object writeObj) {
        ByteArrayOutputStream obj = new ByteArrayOutputStream();
        try {
            ObjectOutputStream out = new ObjectOutputStream(obj);
            out.writeObject(writeObj);
            return obj.toByteArray();

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    //反序列化:

    private static Object derializate(byte[] objs) {
        ByteArrayInputStream bin = new ByteArrayInputStream(objs);
        try {
            ObjectInputStream obin = new ObjectInputStream(bin);
            Object obj = obin.readObject();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static byte[] Bitmap2Bytes(Bitmap bm){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    public static Bitmap Bytes2Bimap(byte[] b){
        if(b.length!=0){
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        }
        else {
            return null;
        }
    }

}
